/*
 * 文件名: SingleWheelLinearLayout.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2012-1-17
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import app.utils.CommonUtils;
import app.utils.Tools;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;
import app.widget.calendar.widget.adapter.DateNumericAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2012-1-17
 * @version [Android PABank C01, @2012-1-17]
 * @description 单条的滚轮 根据传进来的tag判定是每月还是没周
 */
public class SingleWheelLinearLayout extends LinearLayout implements
		OnWheelChangedListener, OnWheelScrollListener {
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * wheel滚动条
	 */
	private WheelView mWheel;
	/**
	 * 标志 表明是每月还是每周
	 */
	private int tag = 1;
	/**
	 * 是否第一次onLayout过
	 */
	private boolean isOnLayout = false;
	/**
	 * 当前值
	 */
	private int curValue;
	/**
	 * 是否处于滚动状态
	 */
	private boolean isScroll = false;
	/**
	 * 当标志为3时 有最大年 最小年的设置
	 */
	private int minYear;
	/**
	 * 最大年
	 */
	private int maxYear;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public SingleWheelLinearLayout(Context context, int tag) {
		super(context);
		this.tag = tag;
		this.context = context;
		init();
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public SingleWheelLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();

	}
	
	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public SingleWheelLinearLayout(Context context,int tag,int minYear,int maxYear) {
		super(context);
		this.context = context;
		this.tag = tag;
		this.minYear = minYear;
		this.maxYear = maxYear;
		init();

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化
	 */
	private void init() {
		setOrientation(LinearLayout.VERTICAL);
		LayoutInflater inflater = LayoutInflater.from(context);
		LinearLayout root = (LinearLayout) inflater.inflate(
				R.layout.app_widget_single_wheel_layout, null);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				Tools.getActualSize(context, 270), LayoutParams.WRAP_CONTENT);
		lp.setMargins(0, Tools.getActualSize(context, 20), 0,
				Tools.getActualSize(context, 20));
		setGravity(Gravity.CENTER);
		root.setLayoutParams(lp);

		addView(root);

		ImageView line = new ImageView(getContext());
		line.setBackgroundResource(R.drawable.app_widget_broken_line_normal);
		LinearLayout.LayoutParams mlp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, Tools.getActualSize(context, 2));
		line.setLayoutParams(mlp);

		addView(line);

		mWheel = (WheelView) findViewById(R.id.wheel);
		mWheel.addChangingListener(this);
		mWheel.addScrollingListener(this);

		//Calendar calendar = Calendar.getInstance();
		Calendar calendar = CommonUtils.getCurrentTimeByCalendar();
		if (tag == 1) {
			int curWeek = calendar.get(Calendar.DAY_OF_WEEK);
			mWheel.setViewAdapter(new DateArrayAdapter(context,
					CalendarDialogCommon.WEEKS, curWeek - 1));
			mWheel.setCurrentItem(curWeek - 1);

			curValue = curWeek - 1;
		} else if (tag == 2) {
			int curDay = calendar.get(Calendar.DAY_OF_MONTH);
			int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			mWheel.setViewAdapter(new DateNumericAdapter(context, 1, maxDays,
					curDay - 1, "日"));
			mWheel.setCurrentItem(curDay - 1);

			curValue = curDay - 1;
		} else if (tag == 3) {
			int curYear = calendar.get(Calendar.YEAR);
			mWheel.setViewAdapter(new DateNumericAdapter(context, minYear , maxYear, curYear - minYear,"年"));
	        mWheel.setCurrentItem(curYear - minYear);
	        curValue = curYear;
		}

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2012-1-17]
	 */
	public interface OnSingleWheelChangeListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * 
		 * @param result
		 *            返回的结果索引
		 */
		void onSingleWheelChange(int result);
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述] 
		 * @param isScroll 滚动状态
		 */
		void onScrollState(boolean isScroll);
	}

	/**
	 * 回调监听器
	 */
	private OnSingleWheelChangeListener onSingleWheelChangeListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] getter
	 * 
	 * @return OnSingleWheelChangeListener
	 */
	public OnSingleWheelChangeListener getOnSingleWheelChangeListener() {
		return onSingleWheelChangeListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onSingleWheelChangeListener
	 *            回调监听器
	 */
	public void setOnSingleWheelChangeListener(
			OnSingleWheelChangeListener onSingleWheelChangeListener) {
		this.onSingleWheelChangeListener = onSingleWheelChangeListener;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		/**
		 * 监听回调
		 */
		if (!isOnLayout) {
			if (onSingleWheelChangeListener != null) {
				onSingleWheelChangeListener.onSingleWheelChange(curValue);
			}
			isOnLayout = true;
		}

	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if (onSingleWheelChangeListener != null) {
			if (tag == 3) {
				onSingleWheelChangeListener.onSingleWheelChange(newValue + minYear);
			} else {
				onSingleWheelChangeListener.onSingleWheelChange(newValue);
			}
			
		}

	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		isScroll = true;
		if (onSingleWheelChangeListener != null) {
			onSingleWheelChangeListener.onScrollState(isScroll);
		}
	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		isScroll = false;
		if (onSingleWheelChangeListener != null) {
			onSingleWheelChangeListener.onScrollState(isScroll);
		}

	}

}
